﻿#include <iostream>
#include "CircularBuffer.h"
#include <cassert>


void test1()
{
    CircularBuffer cb;
    

    // 初始化测试数据
    std::cout << "=== 初始状态 ===\n";
    cb.printfCircularBuffer();

    std::cout << "=== 追加第1组数据 ===\n";
    uint8_t data1[] = { 0x11, 0x22, 0x33 };
    if (cb.append(data1, sizeof(data1))) {
        std::cout << "成功追加了" << sizeof(data1) << " 字节\n";
    }
    else {
        std::cout << "追加失败\n";
    }
    cb.printfCircularBuffer();

    std::cout << "=== 再次追加第2组数据 ===\n";
    uint8_t data2[] = { 0x01, 0x02, 0x03, 0x04 };
    if (cb.append(data2, sizeof(data2))) {
        std::cout << "成功追加了" << sizeof(data2) << " 字节\n";
    }
    else {
        std::cout << "追加失败\n";
    }
    cb.printfCircularBuffer();

    std::cout << "=== 查找目标序列 [0x02, 0x03] ===\n";
    uint8_t target1[] = { 0x02, 0x03 };
    uint8_t* found = cb.find(target1, sizeof(target1));
    if (found) {
        std::cout << "找到了目标序列，位置值：" << static_cast<int>(*found) << "\n";
    }
    else {
        std::cout << "未找到目标序列\n";
    }

    std::cout << "=== 查找目标序列 [0xFF, 0xEE] ===\n";
    uint8_t target2[] = { 0xFF, 0xEE };
    uint8_t* found2 = cb.find(target2, sizeof(target2));
    if (found2) {
        std::cout << "找到了目标序列，位置值：" << static_cast<int>(*found2) << "\n";
    }
    else {
        std::cout << "未找到目标序列\n";
    }

    std::cout << "=== 消费 3 字节 ===\n";
    uint8_t out0[3];
    if (cb.consume(out0, sizeof(out0))) {
        std::cout << "消费成功: ";
        for (int i = 0; i < sizeof(out0); ++i) {
            std::cout << static_cast<int>(out0[i]) << " ";
        }
        std::cout << "\n";
    }
    else {
        std::cout << "消费失败\n";
    }
    cb.printfCircularBuffer();

    std::cout << "=== 消费 4 字节 ===\n";
    uint8_t out1[4];
    if (cb.consume(out1, sizeof(out1))) {
        std::cout << "消费成功: ";
        for (int i = 0; i < sizeof(out1); ++i) {
            std::cout << static_cast<int>(out1[i]) << " ";
        }
        std::cout << "\n";
    }
    else {
        std::cout << "消费失败\n";
    }
    cb.printfCircularBuffer();

    std::cout << "=== 再次追加第3组数据 ===\n";
    uint8_t data3[] = { 'a', 'b','c','d','e','f','g','h','i' };
    if (cb.append(data3, sizeof(data3))) {
        std::cout << "成功追加了" << sizeof(data3) << " 字节\n";
    }
    else {
        std::cout << "追加失败\n";
    }
    cb.printfCircularBuffer();

    std::cout << "头尾" << std::endl;
    std::cout << "begin " << *(cb.begin()) << std::endl;
    std::cout << "end " << *(cb.end()) << std::endl;

    std::cout << "=== 消费 9 字节 ===\n";
    uint8_t out2[9];
    if (cb.consume(out2, sizeof(out2))) {
        std::cout << "消费成功: ";
        for (int i = 0; i < sizeof(out2); ++i) {
            std::cout << static_cast<int>(out2[i]) << " ";
        }
        std::cout << "\n";
    }
    else {
        std::cout << "消费失败\n";
    }
    cb.printfCircularBuffer();

    uint8_t data4[] = { 0x0E, 0x0F };
    std::cout << "=== 再次追加第4组数据 ===\n";
    if (cb.append(data4, sizeof(data4))) {
        std::cout << "成功追加了" << sizeof(data4) << " 字节\n";
    }
    else {
        std::cout << "追加失败\n";
    }
    cb.printfCircularBuffer();

    std::cout << "=== 删除 consumeP 前面的所有数据 ===\n";
    if (cb.eraseFront(cb.begin())) {
        std::cout << "删除成功\n";
    }
    else {
        std::cout << "删除失败\n";
    }
    cb.printfCircularBuffer();

    
    

    std::cout << "=== 清空缓冲区 ===\n";
    cb.clear();
    cb.printfCircularBuffer();

}


void test2()
{
    std::cout << "\n=== Basic Operations Test ===" << std::endl;
    CircularBuffer cb;

    // 测试初始状态
    assert(cb.empty());
    assert(cb.size() == 0);
    assert(cb.capacity() == CIRCULAR_BUFFER_SIZE-1);

    // 测试追加数据
    uint8_t data1[] = { 0x01, 0x02, 0x03 };
    assert(cb.append(data1, sizeof(data1)));
    assert(!cb.empty());
    assert(cb.size() == 3);
    cb.printfCircularBuffer();

    // 测试消费数据
    uint8_t rbuf[3] = { 0 };
    assert(cb.consume(rbuf, 2));
    assert(memcmp(rbuf, data1, 2) == 0);
    assert(cb.size() == 1);
    cb.printfCircularBuffer();

    // 测试清空
    cb.clear();
    assert(cb.empty());


    std::cout << "\n=== Boundary Conditions Test ===" << std::endl;

    // 填满缓冲区
    uint8_t fullData[CIRCULAR_BUFFER_SIZE-1];
    memset(fullData, 0xAA, sizeof(fullData));
    assert(cb.append(fullData, sizeof(fullData)));
    assert(cb.size() == cb.capacity());

    // 测试溢出
    uint8_t overflow = 0xBB;
    assert(!cb.append(&overflow, 1)); // 应失败

    // 测试完全消费
    uint8_t consumeAll[CIRCULAR_BUFFER_SIZE-1] = { 0 };
    assert(cb.consume(consumeAll, cb.capacity()));
    assert(cb.empty());

    // 测试空消费
    assert(!cb.consume(consumeAll, 1)); // 应失败

    cb.clear();

    std::cout << "\n=== Circular Behavior Test ===" << std::endl;
    // 部分填充后消费，制造指针回绕
    uint8_t dataA[] = { 0x10, 0x20, 0x30 };
    uint8_t dataB[] = { 0x40, 0x50, 0x60, 0x70 };

    assert(cb.append(dataA, sizeof(dataA)));
    uint8_t temp[2];
    assert(cb.consume(temp, 2)); // 消费2个

    assert(cb.append(dataB, sizeof(dataB))); // 触发回绕
    assert(cb.size() == 5);
    cb.printfCircularBuffer(); // 应显示 [30 40 50 60 70]

    cb.clear();

    std::cout << "\n=== Find and Erase Test ===" << std::endl;
    uint8_t dataF[] = { 0x55, 0xAA, 0xBB, 0x55, 0xAA, 0xCC };
    assert(cb.append(dataF, sizeof(dataF)));

    // 测试查找
    uint8_t pattern[] = { 0xBB, 0x55 };
    uint8_t* found = cb.find(pattern, sizeof(pattern));
    assert(found != nullptr);
    std::cout << "Found at position: " << (found - cb.begin()) << std::endl;

    // 测试删除前半部分
    assert(cb.eraseFront(found));
    assert(cb.size() == 4);
    cb.printfCircularBuffer(); // 应显示 [BB 55 AA CC]




}


int main() {
    test1();
    /*test2();*/
    return 0;
}